app.controller('myController',
			['$scope', 'todayService', 
			'todayServiceAdvert',
			'liveService','Demo',
			'Live','searchService',
			'Advert',
			'$state',
	 function($scope, 
	 		todayService,
	 		todayServiceAdvert,
	 		liveService,
	 		 Demo,
	 		 Live,
	 		 searchService,
	 		 Advert,$state){
	 		//取回首页菜单的数据
			todayService.then(function(value){
					 $scope.menus = value;
			});
				//取回today页面商品信息数据
			todayServiceAdvert.then(function(value){
					$scope.goodsInfo = value;
					setTimeout(function(){
		            	 var swiper = new Swiper('#bannerBox', {
					        pagination: '.swiper-pagination',
					    
					        autoplay: 1500
					    });       
		            	},100); 
			});
			//生活页的数据
			liveService.then(function(value){
					$scope.liveMenus = value;
			})	
			$scope.demo = new Demo();
			$scope.live = new Live();
			$scope.advert = new Advert();

		searchService.then(function(value){
			$scope.data = value;
				$scope.itemss=value[0].secondCateList;
					var index = 0;
				    $scope.change =  function (e){
				      
				        index = ~~(e.target.id);
				   
				        $scope.itemss = value[index].secondCateList;
				    }

		});
		$scope.toProducter  = function(pId){
			if(cookieObj.get('datas')){
				$scope.number = getTotalCount();
			}else{
				$scope.number = 0;
			}
			
			if(pId.pId){
					$state.go('particulars',{producterId: JSON.stringify(pId)});
			}	
			else{
				pId.pId = pId.goods_id;
				$state.go('particulars',{producterId: JSON.stringify(pId)});
			}	
		}
			//跳转到购物车
		$scope.toCart = function(){
				if(JSON.parse(cookieObj.get('datas')).length){
					$scope.flag1 = true;
					$scope.flag2 =false;
				}else{
					$scope.flag2= true;
					$scope.flag1 =false;
				}
				$state.go('cart',{},{reload:true});
				$scope.pInfo = getAllData();
				$scope.totalMoney = getTotalMoney();
			}
			//添加购物车
		$scope.addCart = function(){
			var count = document.getElementById('cartNumber');
			//查看购物车是否存在
			var listStr = cookieObj.get('datas');
			if(!listStr){
				cookieObj.set({
					name: 'datas',
					value: '[]'
				});
				listStr = cookieObj.get('datas');
			}
			//获取数据转换成一个JSON对象
			var listObj = JSON.parse(listStr);		//循环遍历取出数据的综合
			var totalCount = 0;//购物车默认为零
			for(var i = 0,len = listObj.length;i < len;i++){
				totalCount = listObj[i].pCount + totalCount;
			}
			//商品数量显示到购物车中
			count.innerHTML = totalCount;
			var pic = document.getElementById('introduce_div_img');
			var pId = pic.getAttribute('pid');
		//判断此商品是否在购物车中存在
			if(checkObjByPid(pId)){
					//如存在直接数量加一
					listObj = updateObjById(pId,1);
			}else{
				var cprice = document.getElementById('cprice').innerHTML;
				var oprice = document.getElementById('oprice').innerHTML;
				var pName = document.getElementById('pName').innerHTML;
				//商品对象添加商品信息
				var obj = {};
				obj.pId= pId;
				obj.pImg= pic.src;
				obj.pName= pName;
				obj.cprice=cprice;
				obj.oprice= oprice;
				obj.pCount=1;
			
				listObj.push(obj);
				listObj = updateData(listObj);
			}
			count.innerHTML = getTotalCount();	
		}
		//减少商品数量
		$scope.delOne = function($event){
			var pNum =$event.target.nextSibling.nextSibling;
			var pic = $event.target.parentNode.parentNode.parentNode.previousSibling.previousSibling.children[0];
			var pId = pic.getAttribute('pid');
			console.log(getCountByPId(pId));
			if(getCountByPId(pId)==1){

				$event.target.parentNode.parentNode.parentNode.parentNode.remove($event.target.parentNode.parentNode.parentNode.parentNode);
			}
			
			updateObjById(pId,-1);
			
			if(JSON.parse(cookieObj.get('datas')).length){
			$scope.flag1 = true;
			$scope.flag2 =false;
			}else{
				$scope.flag2= true;
				$scope.flag1 =false;
			}
			$state.go('cart',{reload:true});
			pNum.innerHTML = getCountByPId(pId);
			$scope.totalMoney = getTotalMoney();
		}
		//添加对应商品数量
		$scope.addNum = function($event){
			var pNum =$event.target.previousSibling.previousSibling; 
			var pic = $event.target.parentNode.parentNode.parentNode.previousSibling.previousSibling.children[0];
			var pId = pic.getAttribute('pid');
			updateObjById(pId,1);
			pNum.innerHTML = getCountByPId(pId);
			$scope.totalMoney = getTotalMoney();
		}
	$scope.delAll = function($event){
		var pic = $event.target.parentNode.parentNode.previousSibling.previousSibling.children[0];
		$event.target.parentNode.parentNode.parentNode.remove($event.target.parentNode.parentNode.parentNode);
		var pId = pic.getAttribute('pid');
		deleteObjById(pId);
		if(JSON.parse(cookieObj.get('datas')).length){
			$scope.flag1 = true;
			$scope.flag2 =false;
		}else{
			$scope.flag2= true;
			$scope.flag1 =false;
		}
		$state.go('cart',{reload:true});
		$scope.pInfo = getAllData();
		$scope.totalMoney = getTotalMoney();
	}

	if(cookieObj.get('datas')){
		$scope.flag1 = true;
		$scope.flag2 =false;
		$scope.pInfo = getAllData();
		$scope.totalMoney = getTotalMoney();
	}else{
		$scope.flag2= true;
		$scope.flag1 =false;
	}
	
}])